<?php

/**
 * Created by PhpStorm.
 * User: moting
 * Date: 2017/6/12
 * Time: 15:46
 */
class Controller_Home extends Controller
{

    public function post_getdata()
    {
        //$data = Model_Movie::query()->where('movie_type', '=', Input::post('type'))->order_by('create_time','desc')->limit(8)->offset(0)->get();
        $data = Model_Movie::query()->where(array('movie_type'=>Input::post('type'),'display_sign'=>'checked'))->order_by('create_time','desc')->limit(8)->offset(0)->get();
        $result_array = array();
        $base_uri = \Fuel\Core\Uri::base();
        $i = 0;
        foreach ($data as $data_item)
        {
            $result_array[$i++] = array(
                'id'                => $data_item->id,
                'movie_name'        => $data_item->movie_name,
                'update_time'       => $data_item->update_time,
                'director_name'     => $data_item->director_name,
                'actor_name'        => $data_item->actor_name,
                'movie_type'        => $data_item->movie_type,
                'state_name'        => $data_item->state_name,
                'grade_num'         => $data_item->grade_num,
                'picture_src_path'  => $base_uri . $data_item->picture_src_path,
                'movie_detail'      => $data_item->movie_detail,
                'create_time'       => $data_item->create_time,
                'delete_sign'       => $data_item->delete_sign,
            );
        }
        $result_array = array(
            'type' => Input::post('type'),
            'data' => $result_array
        );
        echo json_encode($result_array);die;
    }

    public function post_getalldata(){
        //$data = Model_Movie::find('all',array('where' => array('movie_type' => Input::post('movie_type'))));
        $data = Model_Movie::query()->where(array('movie_type'=>Input::post('movie_type'),'display_sign'=>'checked'))->order_by('create_time','desc')->get();
        $result_array = array();
        $base_uri = \Fuel\Core\Uri::base();
        //echo $base_uri;die;
        $i=0;
        foreach ($data as $data_item)
        {
            $result_array[$i] = array(
                'id'                => $data_item->id,
                'movie_name'        => $data_item->movie_name,
                'update_time'       => $data_item->update_time,
                'director_name'     => $data_item->director_name,
                'actor_name'        => $data_item->actor_name,
                'movie_type'        => $data_item->movie_type,
                'state_name'        => $data_item->state_name,
                'grade_num'         => $data_item->grade_num,
                'picture_src_path'  => $base_uri . $data_item->picture_src_path,
                'movie_detail'      => $data_item->movie_detail,
                'movie_path'        => $base_uri . $data_item->movie_path,
                'create_time'       => $data_item->create_time,
                'movie_sign'        => $data_item->movie_sign,
                'delete_sign'       => $data_item->delete_sign,
                'display_sign'       => $data_item->display_sign,
            );
            $i++;
        }
        echo json_encode($result_array);die;
    }

    public function action_index()
    {
        $view = View::forge('home/index');

        return $view;
    }

    public function action_movie($movie_type){
        $view = View::forge('home/movie');

        $view->current_type = $movie_type;

        return $view;
    }

    public function action_detail($movie_type, $movie_id)
    {
        $view = View::forge('home/detail');
        $base_uri = \Fuel\Core\Uri::base();
        //echo $base_uri;die;
        $data = Model_Movie::find($movie_id);
        $data->picture_src_path = $base_uri .$data->picture_src_path;
        $data->movie_path = $base_uri . $data->movie_path;
//        $result_array = array(
//            'id'                => $data->id,
//            'movie_name'        => $data->movie_name,
//            'update_time'       => $data->update_time,
//            'director_name'     => $data->director_name,
//            'actor_name'        => $data->actor_name,
//            'movie_type'        => $data->movie_type,
//            'state_name'        => $data->state_name,
//            'grade_num'         => $data->grade_num,
//            'picture_src_path'  => $base_uri . $data->picture_src_path,
//            'movie_detail'      => $data->movie_detail,
//            'movie_path'        => $base_uri . $data->movie_path,
//            'movie_sign'        => $data->movie_sign,
//            'create_time'       => $data->create_time,
//            'delete_sign'       => $data->delete_sign,
//            'display_sign'       => $data->display_sign,
//        );
        //echo $result_array['picture_src_path'];die;
        $view->current_type = $movie_type;
        $view->current_obj = $data;
        return $view;
    }
}